/*
 RLRecyclingRun.m
 Date Created: February 18, 2010
 
 Author: Karol Krizka <kkrizka@gmail.com>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "RLRecyclingRun.h"

#import "RLRecyclingLog.h"

/**
 * Represents a run. It is a collection of items returned during it and a time
 * at which they were returned.
 */
@implementation RLRecyclingRun 

@dynamic dateTime;
@dynamic owner;
@dynamic items;

- (NSUInteger)moneyEarned {
  NSUInteger total=0;
  for(RLRecyclableItem *item in self.items) {
    total+=[item totalValue];
  }
	return total;
}

- (RLRecyclableItem *)itemForUniqueName:(NSString *)uniqueName {
  // Search and return if found
  for(RLRecyclableItem *item in self.items) {
    if([item.type isEqualToString:uniqueName]) {
      return item;
    }
  }
  
  // Item was not found, so we must create a new one
  RLRecyclableItem *item=[NSEntityDescription insertNewObjectForEntityForName:@"RecyclableItem"
                                                       inManagedObjectContext:self.managedObjectContext];
  [item setType:uniqueName];
  [self addItemsObject:item];
  
  return item;
}

@end
